package com.iplant.wms.serviceImpl.dao.brm;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.basicenum.mss.MSSPackageType;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.brm.BRMDistributionProp;
import com.iplant.wms.service.po.brm.BRMDistributionRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

@DBBaseDAO(BRMDistributionProp.class)
public class BRMDistributionPropDAO extends BaseDAO {

    private static BRMDistributionPropDAO Instance = null;
    private static Logger logger = LoggerFactory.getLogger(BRMDistributionPropDAO.class);

    private static int PropTotalMax = 5;

    private BRMDistributionPropDAO() {
        super("");
        this.Init();
    }

    public static BRMDistributionPropDAO getInstance() {
        if (Instance == null) {
            Instance = new BRMDistributionPropDAO();
        }
        return Instance;
    }


    protected void BRM_CreateDistributionProp(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {


        BRMDistributionProp wBRMDistributionProp;
        List<BRMDistributionProp> wBRMDistributionPropList = new ArrayList<>();
        for (MSSPackageType wMSSPackageType : MSSPackageType.values()) {
            if (wMSSPackageType == null || wMSSPackageType.getValue() <= 0) {
                continue;
            }
            wBRMDistributionProp = new BRMDistributionProp();

            wBRMDistributionProp.Name = wMSSPackageType.getLabel();
            wBRMDistributionProp.Code = wMSSPackageType.name();
            wBRMDistributionProp.RuleID = wRuleID;
            wBRMDistributionProp.TypeID = wMSSPackageType.getValue();
            wBRMDistributionProp.LimitType = 0;
            wBRMDistributionProp.OverEnable = 0;
            wBRMDistributionProp.EditorID = wLoginUser.getID();
            wBRMDistributionProp.EditTime = Calendar.getInstance();
            wBRMDistributionProp.CreatorID = wLoginUser.getID();
            wBRMDistributionProp.CreateTime = Calendar.getInstance();
            wBRMDistributionProp.Active = 1;
            wBRMDistributionPropList.add(wBRMDistributionProp);

        }

        this.UpdatePoList(wLoginUser, wBRMDistributionPropList, wErrorCode);

    }

    public List<BRMDistributionProp> BRM_QueryDistributionPropList(
            BMSEmployee wLoginUser, int wRuleID, String wRuleCode, OutResult<Integer> wErrorCode) {

        List<BRMDistributionProp> wBRMDistributionPropList = null;
        try {
            if (wRuleID > 0) {
                wBRMDistributionPropList = this.BRM_QueryDistributionPropList(wLoginUser, wRuleID, "", "", "", -1,
                        Pagination.getMaxSize(), wErrorCode);
            } else if (StringUtils.isNotEmpty(wRuleCode)) {
                wBRMDistributionPropList = this.BRM_QueryDistributionPropList(wLoginUser, -1, wRuleCode, "", "", -1,
                        Pagination.getMaxSize(), wErrorCode);
            } else {
                wErrorCode.Result = ExceptionTypes.Parameter.getValue();
                return new ArrayList<>();
            }


            if (wErrorCode.Result != 0) {
                return wBRMDistributionPropList;
            }

            if (wBRMDistributionPropList != null && wBRMDistributionPropList.size() != PropTotalMax) {

                if (wRuleID <= 0) {
                    BRMDistributionRule mssDistributionRule =
                            BRMDistributionRuleDAO.getInstance().SelectPO(
                                    wLoginUser, -1, wRuleCode, wErrorCode);
                    if (wErrorCode.Result != 0) {
                        return wBRMDistributionPropList;
                    }
                    if (mssDistributionRule == null || mssDistributionRule.ID <= 0) {
                        return wBRMDistributionPropList;
                    }
                    wRuleID = mssDistributionRule.ID;
                }

                Map<String, Object> wDeleteParams = new HashMap<>();
                wDeleteParams.put("RuleID", wRuleID);
                this.Delete(wDeleteParams, " and ID>0 ");

                this.BRM_CreateDistributionProp(wLoginUser, wRuleID, wErrorCode);
                if (wErrorCode.Result != 0) {
                    return wBRMDistributionPropList;
                }


                wBRMDistributionPropList = this.BRM_QueryDistributionPropList(wLoginUser, wRuleID, "", "", "", -1,
                        Pagination.getMaxSize(), wErrorCode);
            }


        } catch (Exception ex) {

            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BRM_QueryDistributionPropList", ex);
        }
        return wBRMDistributionPropList;
    }


    public void BRM_DeleteDistributionProp(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wRuleID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            Map<String, Object> wDeleteParams = new HashMap<>();
            wDeleteParams.put("RuleID", wRuleID);
            this.Delete(wDeleteParams, " and ID>0 ");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BRM_DeleteDistributionProp", ex);
        }
    }


    public List<BRMDistributionProp> BRM_QueryDistributionPropList(
            BMSEmployee wLoginUser, int wRuleID, String wRuleCode, String wName, String wCode, int wActive,
            Pagination wPagination, OutResult<Integer> wErrorCode) {


        Map<String, Object> wParams = new HashMap<>();

        wParams.put("RuleID", wRuleID);
        wParams.put("RuleCode", wRuleCode);
        wParams.put("Name", wName);
        wParams.put("CodeEQ", wCode);

        wParams.put("Active", wActive);


        return this.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);
    }


    public BRMDistributionProp BRM_QueryDistributionProp(
            BMSEmployee wLoginUser, int wRuleID, String wRuleCode, String wCode, OutResult<Integer> wErrorCode) {
        BRMDistributionProp wResult = new BRMDistributionProp();

        try {
            if (StringUtils.isEmpty(wCode) || (wRuleID <= 0 && StringUtils.isEmpty(wCode))) {
                wErrorCode.Result = ExceptionTypes.Parameter.getValue();
                return wResult;
            }

            List<BRMDistributionProp> wBRMDistributionPropList =
                    wBRMDistributionPropList = this.BRM_QueryDistributionPropList(
                            wLoginUser, wRuleID, wRuleCode, "", wCode, -1, Pagination.getDefault(), wErrorCode);


            if (wErrorCode.Result != 0) {
                return wResult;
            }

            if (wBRMDistributionPropList != null && wBRMDistributionPropList.size() > 0) {
                wResult = wBRMDistributionPropList.get(0);
            }


        } catch (Exception ex) {

            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            logger.error("BRM_QueryDistributionProp", ex);
        }
        return wResult;

    }


}
